package com.bnyy.medicalHousekeeper.request;

import android.util.Log;

import com.bnyy.common.ResponseData;
import com.google.gson.Gson;

import rx.Observer;
import rx.functions.Func1;

public class ResponseDataFunc1<T> implements Func1<ResponseData<T>, T> {

    private RequestCallback<T> l;

    public ResponseDataFunc1(Observer<T> o) {
        if (o instanceof  BaseObserver) {
            this.l = (RequestCallback) o;
        }
    }

    @Override
    public T call(ResponseData<T> responseData) {
        Log.e("ResponseDataFunc", new Gson().toJson(responseData));
        int code = responseData.getCode();
        T data = responseData.getData();
        if (l != null) {
            l.onResponse(responseData);
            if (code == 0) {
                l.onSuccess();
                if (data != null) {
                    l.onSuccess(data);
                }
            } else {
                l.onOtherCode(code);
                l.onOtherCode(code, responseData.getMsg());
            }
            if(code == 120) {
                l.relogin();
            }
            l.showMessage(responseData.getMsg());
        }
        return data;
    }
}
